package com.example.rfid_client_volunteer.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;

public class NetHelper {

    private NetHelper(){}

    private static OutputStream os;
    private static PrintWriter pw;
    private static InputStream is;
    private static  InputStreamReader isr;
    private static  BufferedReader br;
    private static Socket socket;


    /*将身份证号发送,得到积分,错误返回-1*/
    public static int write(long id,String serverIP){

        // 连接服务器端
        socket = null;
        try {
            socket = new Socket(serverIP,10086);
            //发送数据
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write(String.valueOf(id));
            pw.flush();
                    is = socket.getInputStream();
                    isr =new InputStreamReader(is);
                    br =new BufferedReader(isr);
                    String info =null;
                    while((info=br.readLine())!=null){
                        System.out.println("收到了数据："+info);
                        //处理JSON数组，得到信息放到界面上去
                    }
            int integral = JsonHelper.getIntegral(info);
        //根据SqliteHelper,找到并调用getjson()返回
         socket.shutdownInput();//关闭输入流
            return integral;
        } catch (Exception e) {
            e.printStackTrace();
        }
      return -1;
    }


}
